Welcome Guest | Sign in | Register

Home > Java Programming > Garbage Collections > Questions and Answers

Exercise:

Section 1

01. public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}

After line 8 runs. how many objects are eligible for garbage collection? 
A. 0 B. 1
C. 2 D. 3

Answer and Explanation

Answer: 1

Explanation:
By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. 2. public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}

When is the Float object, created in line 3, eligible for garbage collection? 
A. just after line 5 B. just after line 6
C. just after line 7 D. just after line 8

Answer and Explanation

Answer: just after line 7

Explanation:
Option A is wrong. This simply copies the object reference into the array.
Option B is wrong. The reference o is set to null, but, oa[0] still maintains the reference to the Float object.
Option C is correct. The thread of execution will then not have access to the object.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7 */

When is the B object, created in line 3, eligible for garbage collection? 
A. after line 5 B. after line 6
C. after line 7 D. There is no way to be absolutely certain.

Answer and Explanation

Answer: There is no way to be absolutely certain.

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Which statement is true?
A. Calling Runtime.gc() will cause eligible objects to be garbage collected. B. The garbage collector uses a mark and sweep algorithm.
C. If an object can be accessed from a live thread, it can't be garbage collected. D. If object 1 refers to object 2, then object 2 can't be garbage collected.

Answer and Explanation

Answer: If an object can be accessed from a live thread, it can't be garbage collected.

Explanation:
This is a great way to think about when objects can be garbage collected.
Option A and B assume guarantees that the garbage collector never makes.
Option D is wrong because of the now famous islands of isolation scenario.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.